package com.ruoyi.APPMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.APPMS.domain.Salesperson;
import com.ruoyi.APPMS.service.ISalespersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工管理Controller
 *
 * @author wanghaoyu
 * @date 2023-07-02
 */
@RestController
@RequestMapping("/salesperson/salesperson")
@Api(tags = "员工管理控制器")
public class SalespersonController extends BaseController {
    @Autowired
    private ISalespersonService salespersonService;

    /**
     * 查询员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('salesperson:salesperson:list')")
    @GetMapping("/list")
    @ApiOperation("分页查询员工信息")
    public TableDataInfo list(Salesperson salesperson) {
        startPage();
        QueryWrapper<Salesperson> queryWrapper = new QueryWrapper<>();
        if (salesperson.getSalespersonname() != null) {
            queryWrapper.like("Salespersonname", salesperson.getSalespersonname());
        }
        if (salesperson.getContactnumber() != null) {
            queryWrapper.like("contactnumber", salesperson.getContactnumber());
        }
        List<Salesperson> list = salespersonService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('salesperson:salesperson:export')")
    @Log(title = "员工管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出员工信息")
    public void export(HttpServletResponse response, Salesperson salesperson) {
        QueryWrapper<Salesperson> queryWrapper = new QueryWrapper<>();
        if (salesperson.getSalespersonname() != null) {
            queryWrapper.like("Salespersonname", salesperson.getSalespersonname());
        }
        if (salesperson.getContactnumber() != null) {
            queryWrapper.like("contactnumber", salesperson.getContactnumber());
        }
        List<Salesperson> list = salespersonService.list(queryWrapper);
        ExcelUtil<Salesperson> util = new ExcelUtil<Salesperson>(Salesperson.class);
        util.exportExcel(response, list, "员工管理数据");
    }

    /**
     * 获取员工管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('salesperson:salesperson:query')")
    @GetMapping(value = "/{salespersonid}")
    @ApiOperation("展示一条员工信息")
    public AjaxResult getInfo(@PathVariable("salespersonid") Long salespersonid) {
        return success(salespersonService.getById(salespersonid));
    }

    /**
     * 新增员工管理
     */
    @PreAuthorize("@ss.hasPermi('salesperson:salesperson:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加员工信息")
    public AjaxResult add(@RequestBody Salesperson salesperson) {
        salesperson.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(salespersonService.save(salesperson));
    }

    /**
     * 修改员工管理
     */
    @PreAuthorize("@ss.hasPermi('salesperson:salesperson:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改员工信息")
    public AjaxResult edit(@RequestBody Salesperson salesperson) {
        salesperson.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(salespersonService.updateById(salesperson));
    }

    /**
     * 删除员工管理
     */
    @PreAuthorize("@ss.hasPermi('salesperson:salesperson:remove')")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{salespersonids}")
    @ApiOperation("删除员工信息")
    public AjaxResult remove(@PathVariable List<Long> salespersonids) {
        return toAjax(salespersonService.removeByIds(salespersonids));
    }

    @PreAuthorize("@ss.hasPermi('supplier:supplier:getSalespersons')")
    @Log(title = "查询所有销售员数据，初始化下拉列表", businessType = BusinessType.DELETE)
    @GetMapping("/getSalespersons")
    @ApiOperation("查询所有员工信息")
    public AjaxResult getSalespersons() {
        return AjaxResult.success(salespersonService.list());
    }
}
